//    Dhammapada PSP
//    Copyright (C) 2007 Sakya
//    sakya_tg@yahoo.it
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include <pspkernel.h>
#include <pspsdk.h>
#include <stdio.h>
#include <stdlib.h>
#include <oslib/oslib.h>

#include "mainmenu.h"
#include "dhammapada.h"
#include "languages.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int mainMenuRetValue = 0;
static int exitFlagMainMenu = 0;
struct settings *globalUserSettings;
struct menuElements tMainMenu;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Continue:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElContinue(){
    mainMenuRetValue = MODE_VIEWCATEGORY;
    exitFlagMainMenu = 1;
    globalUserSettings->intValue01 = tMainMenu.selected;
    globalUserSettings->intValue02 = tMainMenu.first;
    globalUserSettings->previousMode = MODE_MAINMENU;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Options:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElOptions(){
    mainMenuRetValue = MODE_OPTIONS;
    exitFlagMainMenu = 1;
    globalUserSettings->intValue01 = tMainMenu.selected;
    globalUserSettings->intValue02 = tMainMenu.first;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// About:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElAbout(){
    mainMenuRetValue = MODE_ABOUT;
    exitFlagMainMenu = 1;
    globalUserSettings->intValue01 = tMainMenu.selected;
    globalUserSettings->intValue02 = tMainMenu.first;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Index:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElIndex(){
    mainMenuRetValue = MODE_INDEX;
    exitFlagMainMenu = 1;
    globalUserSettings->intValue01 = 0;
    globalUserSettings->intValue02 = 0;
    globalUserSettings->previousMode = MODE_MAINMENU;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Random:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElRandom(){
    int i = 0;
    int count = 0;

    int rnd = getRandomNumber(1, categoriesIndexOriginal.totalSections);
    for (i=0; i<categoriesIndexOriginal.count - 1; i++){
        count += categoriesIndexOriginal.cat[i].sectionsNumber;
        if (count >= rnd)
            break;
    }

    globalUserSettings->lastID = i;
    globalUserSettings->lastSection = categoriesIndexOriginal.cat[i].sectionsNumber - (count - rnd) - 1;
    mainMenuRetValue = MODE_VIEWCATEGORY;
    exitFlagMainMenu = 1;
    globalUserSettings->intValue01 = tMainMenu.selected;
    globalUserSettings->intValue02 = tMainMenu.first;
    globalUserSettings->previousMode = MODE_MAINMENU;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Quit:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menuElQuit(){
    exitFlagMainMenu = 1;
    oslQuit();
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main menu:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int mainMenu(struct settings *userSettings){
    char buffer[262];
    struct menuElement tMenuEl;
    
    globalUserSettings = userSettings;
    mainMenuRetValue = -1;
    //Load images:
    sprintf(buffer, "%s/images/bkg_default.png", userSettings->ebootPath);
    OSL_IMAGE *bkg = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!bkg)
        errorLoadImage(buffer);

    sprintf(buffer, "%s/images/toolbar.png", userSettings->ebootPath);
    OSL_IMAGE *mainToolbar = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!mainToolbar)
        errorLoadImage(buffer);
        
    //Build menu:
    tMainMenu.yPos = 45;
    tMainMenu.xPos = 65;
    tMainMenu.width = 350;
    tMainMenu.height = 200;
    sprintf(buffer, "%s/images/menubkg.png", userSettings->ebootPath);
    tMainMenu.background = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!tMainMenu.background)
        errorLoadImage(buffer);
    tMainMenu.interline = 3;
    tMainMenu.first = 0;
    tMainMenu.selected = 0;
    tMainMenu.numberOfElements = 6;
    tMainMenu.maxNumberVisible = 6;
    tMainMenu.cancelFunction = NULL;
    strcpy(tMenuEl.name, "CONTINUE");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("CONTINUE_HELP"));
    tMenuEl.triggerFunction = menuElContinue;
    tMainMenu.elements[0] = tMenuEl;
    strcpy(tMenuEl.name, "INDEX");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("INDEX_HELP"));
    tMenuEl.triggerFunction = menuElIndex;
    tMainMenu.elements[1] = tMenuEl;
    strcpy(tMenuEl.name, "RANDOM");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("RANDOM_HELP"));
    tMenuEl.triggerFunction = menuElRandom;
    tMainMenu.elements[2] = tMenuEl;
    strcpy(tMenuEl.name, "OPTIONS");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("OPTIONS_HELP"));
    tMenuEl.triggerFunction = menuElOptions;
    tMainMenu.elements[3] = tMenuEl;
    strcpy(tMenuEl.name, "ABOUT");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("ABOUT_HELP"));
    tMenuEl.triggerFunction = menuElAbout;
    tMainMenu.elements[4] = tMenuEl;
    strcpy(tMenuEl.name, "QUIT");
    strcpy(tMenuEl.text, langGetString(tMenuEl.name));
    strcpy(tMenuEl.helpText, langGetString("QUIT_HELP"));
    tMenuEl.triggerFunction = menuElQuit;
    tMainMenu.elements[5] = tMenuEl;

    tMainMenu.selected = globalUserSettings->intValue01;
    tMainMenu.first = globalUserSettings->intValue02;

    exitFlagMainMenu = 0;
    while(!osl_quit && !exitFlagMainMenu){
        oslStartDrawing();

        oslDrawImageXY(bkg, 0, 0);
        oslDrawImageXY(mainToolbar, 0, 272 - mainToolbar->sizeY);
        drawMenu(&tMainMenu);
        
        oslReadKeys();
        processMenuKeys(&tMainMenu);

    	oslEndDrawing();
    	oslSyncFrame();
    	oslAudioVSync();
    }
    //unLoad images:
    clearMenu(&tMainMenu);
    oslDeleteImage(bkg);
    oslDeleteImage(mainToolbar);
    return mainMenuRetValue;
}
